package com.carepad.dao;

import com.carepad.entity.Records;
import com.carepad.entity.RecordsExample;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;

public interface RecordsMapper {

	int countByExample(RecordsExample example);

	int deleteByExample(RecordsExample example);

	int deleteByPrimaryKey(Integer uid);

	int insert(Records record);

	int insertSelective(Records record);

	int insertWithoutId(Records record);

	List<Records> selectByExample(RecordsExample example);

	Records selectByPrimaryKey(Integer uid);

	int updateByExampleSelective(@Param("record") Records record,
			@Param("example") RecordsExample example);

	int updateByExample(@Param("record") Records record,
			@Param("example") RecordsExample example);

	int updateByPrimaryKeySelective(Records record);

	@Select("SELECT tblr.ACTION_TIME_FROM, tblr.ACTION_ITEM, tblr.ANSWER, "
			+ "CASE "
			+ "WHEN EXECUTION_DATE = #{startDate} THEN '1' "
			+ "WHEN EXECUTION_DATE = DATE_ADD(#{startDate}, INTERVAL 1 DAY) THEN '2' "
			+ "WHEN EXECUTION_DATE = DATE_ADD(#{startDate}, INTERVAL 2 DAY) THEN '3' "
			+ "WHEN EXECUTION_DATE = DATE_ADD(#{startDate}, INTERVAL 3 DAY) THEN '4' "
			+ "WHEN EXECUTION_DATE = DATE_ADD(#{startDate}, INTERVAL 4 DAY) THEN '5' "
			+ "WHEN EXECUTION_DATE = DATE_ADD(#{startDate}, INTERVAL 5 DAY) THEN '6' "
			+ "WHEN EXECUTION_DATE = DATE_ADD(#{startDate}, INTERVAL 6 DAY) THEN '7' "
			+ "END AS UID "
			+ "FROM ("
			+ "SELECT tbl_records.ANSWER, EXECUTION_DATE, tbl_records.ACTION_TIME_FROM, tbl_records.ACTION_ITEM  FROM tbl_records WHERE HCS_ID = #{hcsid} AND (EXECUTION_DATE BETWEEN #{startDate} AND #{lastDate} ) UNION ALL "
			+ "SELECT SBP AS ANSWER, EXECUTION_DATE, ACTION_TIME_FROM, CASE WHEN SBP != 'null' THEN '血圧（上）' END AS 'ACTION_ITEM' FROM tbl_vital WHERE HCS_ID = #{hcsid} AND (EXECUTION_DATE BETWEEN #{startDate} AND #{lastDate} ) UNION ALL "
			+ "SELECT DBP AS ANSWER, EXECUTION_DATE, ACTION_TIME_FROM, CASE WHEN DBP != 'null' THEN '血圧（下）' END AS 'ACTION_ITEM' FROM tbl_vital WHERE HCS_ID = #{hcsid} AND (EXECUTION_DATE BETWEEN #{startDate} AND #{lastDate} ) UNION ALL "
			+ "SELECT PR AS ANSWER, EXECUTION_DATE, ACTION_TIME_FROM, CASE WHEN PR != 'null' THEN '脈拍' END AS 'ACTION_ITEM' FROM tbl_vital WHERE HCS_ID = #{hcsid} AND (EXECUTION_DATE BETWEEN #{startDate} AND #{lastDate} ) UNION ALL "
			+ "SELECT BT AS ANSWER, EXECUTION_DATE, ACTION_TIME_FROM, CASE WHEN BT != 'null' THEN '体温' END AS 'ACTION_ITEM' FROM tbl_vital WHERE HCS_ID = #{hcsid} AND (EXECUTION_DATE BETWEEN #{startDate} AND #{lastDate} ) "
			+ ") AS tblr "
			+ "JOIN "
			+ "(SELECT ACTION_ITEM, ACTION_ITEM_ORDER, DISPLAY_GROUP FROM mst_record_items )AS tblmri "
			+ "WHERE tblr.ACTION_ITEM = tblmri.ACTION_ITEM "
			+ "ORDER BY UID, DISPLAY_GROUP, ACTION_ITEM_ORDER, ACTION_TIME_FROM")
	@ResultMap(value = "WeekDataMap")
	List<Records> findRecordListByHcsId(@Param("hcsid") String hcsid,
			@Param("startDate") String startDate,
			@Param("lastDate") String lastDate);

	@Select("SELECT * FROM tbl_records WHERE HCS_ID=#{hcsId} AND EXECUTION_DATE = STR_TO_DATE(#{executionDate},'%m/%d/%Y') AND ACTION_ITEM = #{actionItem}")
	List<Records> findExistingRecord(@Param("sessionId") String sessionId,
			@Param("hcsId") String hcsId,
			@Param("actionItem") String actionItem,
			@Param("executionDate") String date);
}